<?php
namespace admin\service;

/**--tmpl-use--**/
use herosphp\core\Loader;
use herosphp\cache\CacheFactory;
use herosphp\core\WebApplication;

/**--tmpl-import--**/
class NavService extends CommonService
{
    private static $__cacheCtrl;
    private static $__hasModified = false;

    public function __construct($model)
    {
        parent::__construct($model);

        if (self::$__cacheCtrl == null) {
            $config = WebApplication::getInstance()->getConfigs();
            $cachetype = $config['cache_type'];
            self::$__cacheCtrl = CacheFactory::create($cachetype);
        }
    }

    //快速获取导航数据
    public function getAvailNavCached()
    {
        $cache = self::$__cacheCtrl->baseKey('system')->ftype('nav')->factor(appId());
        $v = $cache->get();
        if (!$v) {
            $this->updateCache();
            $cache = self::$__cacheCtrl->baseKey('system')->ftype('nav')->factor(appId());
            $v = $cache->get();
        }
        return $v;

    }

    public function add($data)
    {
        $this->updateCache();
        return parent::add($data);
    }

    public function update($data, $id)
    {
        $result=parent::update($data, $id);
        $this->updateCache();
        return $result;
    }

    public function updates($data, $conditions)
    {
        $this->updateCache();
        return parent::updates($data, $conditions);
    }

    public function deletes($conditions)
    {
        $this->updateCache();
        return parent::deletes($conditions);

    }

    public function delete($id)
    {

        $this->updateCache();
        return parent::delete($id);
    }

    private function updateCache()
    {
        $cache = self::$__cacheCtrl->baseKey('system')->ftype('nav')->factor(appId());
        $items = $this->getItems(array('display' => 1), NULL, array('is_default'=>-1,'listorder'=>1), array(1, C('nav_count')));
        $cache->set(null, $items);

    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        if(!$order['is_default']){
            $order['is_default'] = -1;
        }
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if(!$items) return NULL;

        $manager_managercondi = array();

        foreach($items as $k => $v)
        {
            if($v['manager_id'])
            {
                $manager_managercondi[] = $v['manager_id'];
            }
        }

        $adminDao = Loader::model('adminManager');
        $manager = $adminDao->getItems(array('id' => array('$in' => $manager_managercondi)));
        $typemap = array (
            0 => '文字',
            1 => '图片',
        );
        foreach ($items as $k => $v)
        {
            foreach($manager as $kk => $vv)
            {
                if($vv['id'] == $v['manager_id'])
                {
                    $items[$k]['manager_name'] = $vv['name'];
                }
            }

            $items[$k]['type_text'] = $typemap[$v['type']];

        }
        return $items;
    }
}

